const {
  CreateService,
  UpdateService,
  DeleteService,
  findService,
  UpdateFatherService,
  restoreService,
  reNameService,
  getPathService,
  getInfoService,
} = require("../Service/document.service");
const {
  updatesoftError,
  removeDocError,
  restoreDocError,
  findDocError,
} = require("../Constant/err.type");

class DocumentController {
  async create(ctx, next) {
    const res = await CreateService(ctx, ctx.request.body);
    // 创建文件之后会返回一个文件id 拿到这个id 去跟新父亲节点上的文件id
    const { id, fatherId } = res;
    const resF = await UpdateFatherService(id, fatherId);
    ctx.body = {
      code: 0,
      result: res,
      message: "创建文件成功",
    };
  }
  async remove(ctx, next) {
    const id = ctx.request.params.id;
    const res = await DeleteService(id);
    if (res) {
      ctx.body = {
        code: 0,
        message: "删除文件成功",
        res,
      };
    } else {
      return ctx.app.emit("error", removeDocError, ctx);
    }
  }
  async find(ctx, next) {
    const res = await findService(ctx.request.params.id);
    if (res) {
      ctx.body = {
        code: 0,
        message: "查询文件成功",
        result: res,
      };
    } else {
      return ctx.app.emit("error", findDocError, ctx);
    }
  }
  async update(ctx, next) {}
  async restore(ctx) {
    const res = await restoreService(ctx.params.id);
    if (res) {
      ctx.body = {
        code: 0,
        message: "恢复文件成功",
        result: "",
      };
    } else {
      return ctx.app.emit("error", restoreDocError, ctx);
    }
  }
  async reName(ctx, next) {
    const id = ctx.request.params.id;
    const res = await reNameService(id, ctx.request.body);
    if (res) {
      ctx.body = {
        code: 0,
        message: "重命名文件成功",
        result: "",
      };
    } else {
      ctx.body = {
        code: 0,
        message: "重命名文件shibai",
        result: "",
      };
    }
  }
  async getPath(ctx) {
    const res = await getPathService(ctx.request.params.id);
    ctx.body = {
      code: 0,
      message: "获取地址成功",
      res: res,
    };
  }
  async getInfo(ctx) {
    const res = await getInfoService(ctx.request.params.id);
    ctx.body = {
      code: 0,
      message: "获取文件信息成功",
      res: res,
    };
  }
}

module.exports = new DocumentController();
